//
//  WBNavigationController.swift
//  微博
//
//  Created by 胡宁 on 2016/12/10.
//  Copyright © 2016年 胡宁. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBar.isHidden = true
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        
        if let vc = viewController as? WBBaseViewController {
            
            var title = ""
            var isBack = false
            
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.title ?? "返回"
                isBack = true
            }else if childViewControllers.count > 1{
                title = "返回"
                isBack = true
            }
            
            vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack: isBack)
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    @objc private func popToParent() {
        popViewController(animated: true)
    }
}
